package logic

import (
	"context"
	"fmt"
	"zhihu/models"

	"zhihu/zhihu_rpc/internal/svc"
	"zhihu/zhihu_rpc/zhihu_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PushTroubleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPushTroubleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PushTroubleLogic {
	return &PushTroubleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PushTroubleLogic) PushTrouble(in *zhihu_rpc.PushTroubleRequest) (*zhihu_rpc.PushTroubleResponse, error) {
	// todo: add your logic here and delete this line
	trouble := models.Trouble{
		UserId:  int(in.Id),
		Title:   in.Title,
		Content: in.Content,
		Hot:     0,
	}
	if trouble.Content == "" {
		return &zhihu_rpc.PushTroubleResponse{}, fmt.Errorf("内容不得为空")
	}
	if trouble.Title == "" {
		return &zhihu_rpc.PushTroubleResponse{}, fmt.Errorf("标题不得为空")
	}
	if models.DB.Create(&trouble).Error != nil {
		return &zhihu_rpc.PushTroubleResponse{}, fmt.Errorf("服务器繁忙")
	}
	log := models.UserLog{
		UserId: int(trouble.UserId),
		Des:    "push his trouble",
	}
	if models.DB.Create(&log).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &zhihu_rpc.PushTroubleResponse{}, nil
}
